"""
  Copyright (c) 2024 Northeastern University (China)
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
"""
import logging
from ..extractors.extractor import Extractor
from ..extractors.dependency import Dependency
from ..utils.utils import read_js

logging.basicConfig()
logger = logging.getLogger(__name__)

    
class VcpkgExtractor(Extractor):
    def __init__(self, target) -> None:
        super().__init__()
        self.type = 'vcpkg'
        self.target = target

    def run_extractor(self):
        self.process_vcpkg()

    def process_vcpkg(self):
        content = read_js(self.target)
        # TODO add dependencies in feature field
        if 'dependencies' in content:
            for dependency in content['dependencies']:
                if isinstance(dependency, str):
                    dep = Dependency(dependency, None)
                    dep.add_evidence(self.type, self.target, 'High')
                    self.add_dependency(dep)
                elif isinstance(dependency, dict):
                    if 'name' not in dependency:
                        continue
                    if 'version' in dependency:
                        version = dependency['version']
                    else:
                        version = None
                    dep = Dependency(dependency['name'], version)
                    dep.add_evidence(self.type, self.target, 'High')
                    self.add_dependency(dep)